import os


def printfilelist(gdir):
    # 首先获得文件列表
    dirlist = os.listdir(gdir)
    filesizelist = []
    # 遍历列表获得文件名称
    for filename in dirlist:
        filepath = os.path.join(gdir, filename)

        if os.path.isfile(filepath):
            print("{:>8}".format(os.path.basename(filepath)))
            filesizelist.append(os.path.getsize(filepath))

        if os.path.isdir(filepath):
            print(os.path.basename(filepath), "---------------->dir")
            filesizelist.append(printfilelist(filepath))

    return filesizelist


def addlist(numlist):
    sum = 0
    # 遍历列表的每一项，并求和
    for item in numlist:
        if type(item) == int:
            sum = sum + item
        if type(item) == list:
            sum = sum + addlist(item)
    return sum


numlist = printfilelist("F:\CSDN\demo")
print("==" * 20)
print(numlist)
print("==" * 20)
print(addlist(numlist))
